package com.e_manager.soft.emanager.service.impl;

import com.e_manager.soft.emanager.entity.MstImage;
import com.e_manager.soft.emanager.entity.parameter.MstImageParameter;
import com.e_manager.soft.emanager.idao.MstImageDao;
import com.e_manager.soft.emanager.resultMsg.ResultConstant;
import com.e_manager.soft.emanager.resultMsg.ResultInfo;
import com.e_manager.soft.emanager.service.MstImageService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * [MstImageServiceImpl]<br>
 * 轮播图信息<br>
 * <br>
 * [功能概要]<br>
 * 轮播图信息<br>
 * <br>
 * [更新履历]<br>
 * 2018/03/15 ver1.00 JYS CORPORATION<br>
 *
 * @author JYS CORPORATION
 * @version 1.00
 */
@Service
public class MstImageServiceImpl implements MstImageService {
    @Autowired
    private MstImageDao mstImageDao;

    /**
     * 轮播图列表
     *
     * @param t
     * @return
     */
    @Override
    public PageInfo<MstImage> findAllByPage(MstImageParameter t) {
        PageInfo<MstImage> info = null;
        try {
            // 分页
            Page<MstImage> tPage = PageHelper.startPage(t.getPageNumber(), t.getPageSize());
            // 获取列表
            List<MstImage> findImageList = mstImageDao.findAllByPage(t);
            // 获取返回分页数据
            info = new PageInfo(findImageList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 添加轮播图信息
     *
     * @param mstImage
     * @return
     */
    @Override
    public ResultInfo<?> save(MstImage mstImage) {
        //初始化对象
        ResultInfo info = new ResultInfo();
        try {
            if (mstImage.getId() != 0) {
                mstImageDao.updateById(mstImage);
            } else {
                //插入信息
                mstImageDao.save(mstImage);
            }
            //返回成功编码
            info.setMsg(ResultConstant.SUCCESS.getMessage());
            info.setCode(ResultConstant.SUCCESS.getCode());
        } catch (Exception e) {
            info.setMsg(ResultConstant.FAIL.getMessage());
            info.setCode(ResultConstant.FAIL.getCode());
        }

        return info;
    }

    /**
     * 更新状态
     *
     * @param mstImage
     * @return
     */
    @Override
    public ResultInfo<?> updateByStatus(MstImage mstImage) {
        //初始化对象
        ResultInfo info = new ResultInfo();
        try {
            if (mstImage.getId() != 0) {
                mstImageDao.updateByStatus(mstImage);
                //返回成功编码
                info.setMsg(ResultConstant.SUCCESS.getMessage());
                info.setCode(ResultConstant.SUCCESS.getCode());
            }
        } catch (Exception e) {
            info.setMsg(ResultConstant.FAIL.getMessage());
            info.setCode(ResultConstant.FAIL.getCode());
        }
        return info;
    }
}